package io.noties.markwon.base.html


class HtmlEmptyTagFormatReplacement {
    /**
     * @return replacement for supplied startTag or null if no replacement should occur (which will
     * lead to `Inline` tag have start &amp; end the same value, thus not applicable for applying a Span)
     */
    fun replace(tag: HtmlTag): String? {
        val replacement: String?
        val name = tag.name()
        replacement = if ("br" == name) {
            "\n"
        } else if ("img" == name) {
            val alt = tag.attributes()["alt"]
            if (alt == null || alt.isEmpty()
            ) {
                // no alt is provided
                IMG_REPLACEMENT
            } else {
                alt
            }
        } else if ("video" == name) {
            // @since 4.4.0 make iframe non-empty
            VIDEO_REPLACEMENT
        } else if ("audio" == name) {
            // @since 4.4.0 make iframe non-empty
            AUDIO_REPLACEMENT
        } else if ("iframe" == name) {
            // @since 4.4.0 make iframe non-empty
            IFRAME_REPLACEMENT
        } else {
            null
        }
        return replacement
    }

    companion object {
        fun create(): HtmlEmptyTagFormatReplacement {
            return HtmlEmptyTagFormatReplacement()
        }

        private const val IMG_REPLACEMENT = "\uFFFC"
        private const val IFRAME_REPLACEMENT = "\u00a0" // non-breakable space
        private const val VIDEO_REPLACEMENT = "\u00a0" // non-breakable space
        private const val AUDIO_REPLACEMENT = "\u00a0" // non-breakable space
    }
}